<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User-Edit-Page</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/myJs/myDate.js"></script>
<script type="text/javascript">	
	function user_submit(){
		var count=1;
		var isNo = true;
		var un = document.getElementById("tblUser.username").value;
		var un_reg = new RegExp(/^[a-zA-z][a-zA-Z0-9_]{2,9}$/);
		var pw = document.getElementById("tblUser.password").value;
		var pw_2 = document.getElementById("password_two").value;
		var pw_reg = new RegExp(/^[A-Za-z]+[0-9]+[A-Za-z0-9]*|[0-9]+[A-Za-z]+[A-Za-z0-9]*$/g);
		var fn = document.getElementById("tblPi.fullName").value;
		var fn_reg = new RegExp(/[\u4e00-\u9fa5]{2,}/);
		var sex = document.getElementById("tblPi.gender").selectedIndex;
		var bd = document.getElementById("tblPi.birthday").value;
		var em = document.getElementById("tblPi.eamil").value;
		var em_reg = new RegExp(/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/);
		if(un!=''&&un_reg.test(un)){
			$.ajax({
				type:'post',
				url:'user_checkLegitimacy',
				data:'username='+un,
				dataType:'json',
				error:function(){
					document.getElementById("un_s").innerHTML="服务器内部错误！";
				},
				success:function(data){
					if(data.user_checkLegitimacy == 1){
						count++;
						clearText("un_s");
					}else if(data.user_checkLegitimacy == 0){
						document.getElementById("un_s").innerHTML="用户名已存在，请重新输入。";
					}
				}
			});
		}else{
			document.getElementById("un_s").innerHTML="用户名长度必须大于2，且只能是大小写字母和数字！";
		}
		if(pw!=''&&pw_reg.test(pw)){
			clearText("pw_s");
			if(pw.localeCompare(pw_2)==0){
				count++;
				clearText("pw_2_s");
			}else{
				document.getElementById("pw_2_s").innerHTML="两次输入的密码不一样！";
			}
		}else{
			document.getElementById("pw_s").innerHTML="密码必须由6-16个英文字母和数字字符串组成！";
		}
		if(fn!='' && fn_reg.test(fn)){
			count++;
			clearText("fn_s");
		}else{
			document.getElementById("fn_s").innerHTML="输入的姓名不合法，请重新输入。";
		}
		if(sex!=0){
			count++;
			clearText("sex_s");
		}else{
			document.getElementById("sex_s").innerHTML="请选择性别~";
		}
		if(bd!=''){
			count++;
			clearText("day_s");
		}else{
			document.getElementById("day_s").innerHTML="请输入日期~";
		}
		if(em!=''){
			if(!em_reg.test(em)){
				document.getElementById("em_s").innerHTML="输入数据不合法！";	
				isNo = false;
			}else{
				document.getElementById("em_s").innerHTML="";
				isNo = true;
			}
		}
		if(count==5&&isNo){
			
			document.getElementById("user_add_form").submit();
		}
	}
	
	function clearText(condition){
		document.getElementById(condition).innerHTML="*";
	}
</script>
</head>
<body>
	<div>
		<form action="user_do_add" method="post" id="user_add_form" >
			<div>
				<label>用户名：</label>
				<input type="text" id="tblUser.username" name="tblUser.username" />
				<span id="un_s" style="color:red;">*</span>
			</div>
			<div>
				<label>密码：</label>
				<input type="password" id="tblUser.password" name="tblUser.password" />
				<span id="pw_s" style="color:red;">*</span>
			</div>
			<div>
				<label>确认密码：</label>
				<input type="password" id="password_two" />
				<span id="pw_2_s" style="color:red;">*</span>
			</div>
			<div id="tblPi.form" >
				<div>
					<label>姓名：</label>
					<input type="text" id="tblPi.fullName" name="tblPi.fullName" />
					<span id="fn_s" style="color:red;">*</span>
				</div>
				<div>
					<label>性别：</label>
					<s:select id="tblPi.gender" name="tblPi.gender" list="#{0:'-----选择性别-----',1:'男',2:'女'}" listKey="key" listValue="value"/>
					<span id="sex_s" style="color:red;">*</span>
				</div>
				<div>
					<label>出生年月日：</label>
					<input id="tblPi.birthday" name="tblPi.birthday" type="text" onclick="fPopCalendar(event,this,this)" onfocus="this.select()"/>
					<span id="day_s" style="color:red;">*</span>
				</div>
				<div>
					<label>phone</label>
					<input id="tblPi.phone" name="tblPi.phone" onkeyup="this.value=this.value.replace(/\D/g,'')" 
						onafterpaste="this.value=this.value.replace(/\D/g,'')" maxlength="11"/>
				</div>
				<div>
					<label>Email:</label>
					<input type="text" id="tblPi.eamil" name="tblPi.email" />
					<span id="em_s" style="color:red;"></span>
				</div>
				</div>
			<div>
				<input type="button" value="保存添加" onclick="user_submit()" />
			</div>
		</form>
	</div>
</body>
</html>